import sys, pygame, random, colorlib

maxSpeed = [maxX, maxY] = [10, 10]

class Window():
  def __init__(self):
    self.size = self.width, self.height = 400, 600
    self.screen = pygame.display.set_mode(self.size)
    #self.font = pygame.font.Font(None, 25)
    self.clock = pygame.time.Clock()
    self.bgcolor = colorlib.random_color('dark')
  
  def change_color(self):
    self.bgcolor = colorlib.random_color('dark')
    
  def draw(self, ball):
    self.screen.fill(self.bgcolor)
    pygame.draw.circle(self.screen, ball.getColor(), ball.getPos(), 20)
    pygame.display.flip()
    self.clock.tick(60)
  
  def get_events(self, ball):
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
	sys.exit()
      if event.type == pygame.KEYDOWN:
	if event.key == pygame.K_DOWN:
	  ball.ballSpeed[1] += 1
	if event.key == pygame.K_UP:
	  ball.ballSpeed[1] -= 1
	if event.key == pygame.K_LEFT:
	  ball.ballSpeed[0] -= 1
	if event.key == pygame.K_RIGHT:
	  ball.ballSpeed[0] += 1

class Ball():
  def __init__(self):
    self.ballPos = [100, 200]
    self.ballSpeed = [10, 10]
    self.ballColor = colorlib.random_color('white')
    
  def updatePosAndColor(self, window):
    self.ballPos[0] += self.ballSpeed[0]
    self.ballPos[1] += self.ballSpeed[1]
    if self.ballPos[0] < 20 or self.ballPos[0] > window.width-20:
      self.ballSpeed[0] = -self.ballSpeed[0]
      self.ballColor = colorlib.random_color('white')
      window.change_color()
      
    if self.ballPos[1] < 20 or self.ballPos[1] > window.height-20:
      self.ballSpeed[1] *= -1
      self.ballColor = colorlib.random_color('white')
      window.change_color()
      
    if self.ballSpeed[0] > maxX: self.ballSpeed[0] = maxX
    if self.ballSpeed[1] > maxY: self.ballSpeed[1] = maxY
    if self.ballSpeed[0] == 0: self.ballSpeed[0] = random.randrange(-1, 2)
    if self.ballSpeed[1] == 0: self.ballSpeed[1] = random.randrange(-1, 2)
    
  def getPos(self):
    return self.ballPos
    
  def getColor(self):
    return self.ballColor
  